package com.oceansoft.core.jxkh.dao.impl;

import com.oceansoft.core.common.util.PageUtil;
import com.oceansoft.core.common.util.Pagination;
import com.oceansoft.core.jxkh.dao.IKhlxDao;
import com.oceansoft.core.jxkh.domain.Khlx;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;

/**
 * Created by OS on 2017/12/13.
 */

@Repository
public class KhlxDaoImpl implements IKhlxDao{

    @Resource
    private JdbcTemplate jdbcTemplate;

    @Override
    public Pagination findAllKhlx(HashMap<String, String> map) {
        int offset = Integer.parseInt(map.get("offset").toString());
        int limit = Integer.parseInt(map.get("limit").toString());
        String sql = "select b.dbid,b.itemaId,b.belong,b.name,b.score,b.status,a.name khyName from t_pskh_check_itemb b inner join t_pskh_check_itema a on b.itemaId = a.dbid order by b.dbid ";
        String count_sql = PageUtil.getCountSQL(sql.toString());
        int _total = jdbcTemplate.queryForInt(count_sql);
        String page_sql = PageUtil.getPageSQL(sql.toString(), offset,limit);
        List list = jdbcTemplate.queryForList(page_sql);
        return new Pagination(list,_total,limit, (offset/limit)+1);
    }

    @Override
    public void editKhlxInfo(Khlx khlx) {
        String sql = "update t_pskh_check_itemb set itemaId = ? , belong = ? , name = ? , score = ? , status = ? where dbid = ?";
        jdbcTemplate.update(sql,khlx.getItemaId(),khlx.getBelong(),khlx.getName(),khlx.getScore(), khlx.getStatus(),khlx.getDbid());
    }

    @Override
    public Khlx findKhlxInfoByDbid(int dbid) {
        String sql = "select b.dbid,b.itemaId,b.belong,b.name,b.score,b.status,a.name khyName " +
                "from t_pskh_check_itemb b inner join t_pskh_check_itema a on b.itemaId = a.dbid where b.dbid = ?";
        Khlx khlx = jdbcTemplate.queryForObject(sql, new Object[]{dbid}, new RowMapper<Khlx>() {
            @Override
            public Khlx mapRow(ResultSet rs, int args) throws SQLException {
                Khlx khlx = new Khlx();
                khlx.setDbid(rs.getInt("dbid"));
                khlx.setItemaId(rs.getInt("itemaId"));
                khlx.setBelong(rs.getString("belong"));
                khlx.setName(rs.getString("name"));
                khlx.setScore(rs.getInt("score"));
                khlx.setStatus(rs.getInt("status"));
                khlx.setKhyName(rs.getString("khyName"));
                khlx.setBelong(rs.getString("belong"));
                return khlx;
            }
        });
        return khlx;
    }

    @Override
    public void deleteKhlxInfoByDbid(int dbid) {
        String sql = "delete from t_pskh_check_itemb where dbid = ?";
        jdbcTemplate.update(sql, new Object[]{dbid});
    }

    @Override
    public void addKhlxInfo(Khlx khlx) {
        String sql = "insert into t_pskh_check_itemb(dbid,itemaId,belong,name,score,status) values(SEQ_PSKH_CHECK_ITEMB.NEXTVAL,?,?,?,?,?)";
        jdbcTemplate.update(sql,khlx.getItemaId(),khlx.getBelong(),khlx.getName(),khlx.getScore(),khlx.getStatus());
    }

    @Override
    public void batchDeleteKhlxInfo(final int[] dbids) {
        String sql = "delete from t_pskh_check_itemb where dbid = ?";
        int[] delCounts = jdbcTemplate.batchUpdate(sql, new BatchPreparedStatementSetter() {
            @Override
            public void setValues(PreparedStatement ps, int i) throws SQLException {
                ps.setInt(1,dbids[i]);
            }
            @Override
            public int getBatchSize() {
                return dbids.length;
            }
        });
    }

    @Override
    public List findAllKhlxName() {
        String sql = "select name,dbid from t_pskh_check_itemb";
        List list = jdbcTemplate.queryForList(sql);
        return list;
    }

    @Override
    public List findKhlxNameByItemaId(int itemaId) {
        String sql = "select name,dbid from t_pskh_check_itemb where itemaId = ?";
        List list = jdbcTemplate.queryForList(sql,itemaId);
        return list;
    }
}
